// api/session_api/session_create.go
package session_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

type SessionDetailResponse struct {
	models.SessionModel
	RoleTitle         string `json:"roleTitle"`
	RoleAvatar        string `json:"roleAvatar"`
	RoleAbstract      string `json:"roleAbstract"`
	RoleUserChatCount int64  `json:"roleUserChatCount"` // 这个会话中的角色，我使用它创建的对话条数
}

func (SessionApi) SessionDetailView(c *gin.Context) {
	var cr models.IDRequest
	err := c.ShouldBindUri(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	var session models.SessionModel
	err = global.DB.Preload("RoleModel").Take(&session, cr.ID).Error
	if err != nil {
		res.FailWithMsg(c, "会话不存在")
		return
	}

	data := SessionDetailResponse{
		SessionModel: session,
		RoleTitle:    session.RoleModel.Title,
		RoleAvatar:   session.RoleModel.Avatar,
		RoleAbstract: session.RoleModel.Abstract,
	}

	global.DB.Model(models.ChatModel{}).
		Where("user_id = ? and role_id = ?", claims.UserID, session.RoleID).Count(&data.RoleUserChatCount)

	res.OkWithData(c, data)
}
